package com.pay.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pay.common.pay.constant.Constant;
import com.pay.common.pay.utils.RedisUtils;
import com.pay.common.pay.utils.TokenUtils;
import com.pay.controller.open.api.dto.req.PaymentInfoDTO;
import com.pay.entity.PaymentInfoDO;
import com.pay.mapper.PaymentInfoMapper;
import com.pay.service.open.api.OpenPaymentCoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 周益和
 * @ClassName OpenPaymentCoreServiceImpl
 * @qq 1552951165
 */
@Service
public class OpenPaymentCoreServiceImpl implements OpenPaymentCoreService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    
    /**
     * 创建PayToken
     * @param paymentInfoDTO
     * @return
     */
    @Override
    public String createPayToken(PaymentInfoDTO paymentInfoDTO) {
        // 1.将PaymentInfoDTO转化成PaymentInfoDO
        PaymentInfoDO paymentInfoDO = new PaymentInfoDO();
        BeanUtils.copyProperties(paymentInfoDTO, paymentInfoDO);
        // 2.插入数据
        int insert = paymentInfoMapper.insert(paymentInfoDO);
        if (!(insert > Constant.RESULT_INSERT_ZERO)) {
            return Constant.RESULT_STRING_NULL;
        }
        // 3.生成对应的支付令牌
        // 获取主键id作为Redis中的值
        Integer payId = paymentInfoDO.getId();
        
        // 通过前缀生成带有前缀的token--支付令牌
        String payToken = TokenUtils.getToken(Constant.PAY_PREFIX);
        
        RedisUtils.setString(payToken, String.valueOf(payId));
        return payToken;
    }
    
    /**
     * 根据支付令牌获取支付的参数信息
     * @param payToken
     * @return
     */
    @Override
    public PaymentInfoDO getPayTokenPaymentInfo(String payToken) {
        // 在Redis中根据payToken获取到用户的payIdStr
        String payIdStr = RedisUtils.getString(payToken);
        if (StringUtils.isEmpty(payIdStr)) {
            return null;
        }
        // 转换成int类型的
        Integer payId = Integer.parseInt(payIdStr);
        // 根据payid查询支付的信息
        PaymentInfoDO paymentInfoDo = paymentInfoMapper.selectById(payId);
        return paymentInfoDo;
    }
    
    /**
     * 根据订单号获取支付信息
     * @param outTradeNo
     * @return
     */
    @Override
    public PaymentInfoDO getPaymentInfoByOrderId(String orderId) {
        QueryWrapper<PaymentInfoDO> qw = new QueryWrapper<>();
        qw.eq("order_id", orderId);
        PaymentInfoDO paymentInfoDO = paymentInfoMapper.selectOne(qw);
        return paymentInfoDO;
    }
    
    
    /**
     * 更新支付状态
     * @param paymentInfo
     * @param payPaymentCompleted
     * @return
     */
    @Override
    public int updatePaymentStatus(PaymentInfoDO paymentInfo, Integer paymentStatus) {
        paymentInfo.setPaymentStatus(paymentStatus);
        int result = paymentInfoMapper.updateById(paymentInfo);
        return result;
    }
    
    /**
     *  更新支付状态为成功
     * @param paymentInfo
     * @return
     */
    @Override
    public int updatePaymentStatusOk(PaymentInfoDO paymentInfo) {
        // 将订单信息--支付状态设置为成功
        paymentInfo.setPaymentStatus(Constant.PAY_PAYMENT_COMPLETED);
        int result = paymentInfoMapper.updateById(paymentInfo);
        return result;
    }
}
